package com.rr.graphql.resolver;

import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsMutation;
import com.netflix.graphql.dgs.InputArgument;
import com.rr.dto.Employee;
import com.rr.graphql.input.EmployeeInput;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author RR
 * @date 2023/6/11 17:25
 */


@DgsComponent
public class EmployeeMutationResolver{

    @DgsMutation
    public Employee createEmployee(@InputArgument(name = "input") EmployeeInput input) {
        Employee employee = new Employee();
        employee.setId(UUID.randomUUID().toString());
        employee.setName(input.getName());
        employee.setSex(input.getSex());
        employee.setBirthday(input.getBirthday());
        employee.setDepartmentId(input.getDepartmentId());
        return employee;
    }

    @DgsMutation
    public List<Employee> createEmployees(@InputArgument(name = "input", collectionType = EmployeeInput.class)
                                              List<EmployeeInput> input) {
        int[] a = new int[]{0};
        return input.stream().map(item -> {
            Employee employee = new Employee();
            employee.setDepartmentId(item.getDepartmentId());
            employee.setId(String.valueOf(a[0]++));
            employee.setName(item.getName());
            employee.setSex(item.getSex());
            employee.setBirthday(item.getBirthday());
            employee.setDepartmentId(item.getDepartmentId());
            return employee;
        }).collect(Collectors.toList());
    }

}
